﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Collections;

namespace SkyBase
{
    class Player : GameObject
    {

        public Item[] Inventory { get; set; } 
        public Texture2D playerSprite {get; set;}
        public Rectangle locationRec { get; set; }
        public Texture2D weaponSprite { get; set; }
        public Rectangle weaponLocation { get; set; }
        public double weaponRotation { get; set; }
        Vector2 weaponSlope;
        public Weapon equippedWeapon { get; set; }
        public int Health = 30;
        public int maxHealth { get; set; }
        
        public Player(String nameIn, World worldIn, Texture2D spriteIn = null)
            :base(nameIn, new Vector2((worldIn.GetEngine().viewportSize.Width/2)-(spriteIn.Width/2),(worldIn.GetEngine().viewportSize.Height/2)-(spriteIn.Height/2)), spriteIn, true, worldIn)
        {
            locationRec = new Rectangle((worldIn.GetEngine().viewportSize.Width/2) /*- (spriteIn.Width/2 )*/, (worldIn.GetEngine().viewportSize.Height/2) /*- (spriteIn.Height / 2)*/, spriteIn.Width, spriteIn.Height);
            Inventory = new Item[9];
            //loadRandomInventory();
            weaponLocation = new Rectangle(locationRec.Center.X - 16, locationRec.Center.Y - 16, 32, 32);
            Weapon startWeapon = new Weapon("Starting Gun", "Trusty starting gun", "ranged", 3, Storage.getTexture("Gun"), Storage.getTexture("GunEquip"), World, 5, 3, 0, 0);
            //startWeapon.Name = "Starting Gun";
            Inventory[0] = startWeapon;
            equippedWeapon = (Weapon)Inventory[0];
            weaponSprite = startWeapon.EquipSprite;
            startWeapon.InventoryColor = Color.Yellow;
            checkForCollision = true;
            drawBounds = false;
            priority = 0.2f;
            maxHealth = Health;

            Debugger.enabled = true;
        }

        new public void Update() 
        {
            updateTextures("player");
            Vector2 mousePoint = new Vector2(Mouse.GetState().X, Mouse.GetState().Y);
            if (!World.GetEngine().jumping)
            {
                if (VX > 5) VX = 5;
                if (VX < -5) VX = -5;
                if (VY > 5) VY = 5;
                if (VY < -5) VY = -5;

                if (Math.Sqrt((VX * VX) + (VY * VY)) > 5.0)
                {
                    if (VX > 0) VX = (float)Math.Sqrt(12.5);
                    else VX = -(float)Math.Sqrt(12.5);
                    if (VY > 0) VY = (float)Math.Sqrt(12.5);
                    else VY = -(float)Math.Sqrt(12.5);
                }
            }
            locationRec = new Rectangle((int)Location.X, (int)Location.Y, 64, 64);
            weaponSlope = new Vector2((mousePoint.X + -World.GetEngine().GetCameraPos().X) - locationRec.Center.X, (mousePoint.Y + -World.GetEngine().GetCameraPos().Y) - locationRec.Center.Y);
            double actualweaponSlope = weaponSlope.Y / weaponSlope.X;
            weaponRotation = (Math.Atan(actualweaponSlope) + (Math.PI / 2) + (Math.PI/6));
            if ((weaponSlope.Y > 0 && weaponSlope.X < 0) || (weaponSlope.Y<0 && weaponSlope.X<0)) weaponRotation += Math.PI;
            //weaponRotation += (Math.PI / 4);
            weaponLocation = new Rectangle(locationRec.Center.X - 16, locationRec.Center.Y - 16, equippedWeapon.EquipSprite.Width, equippedWeapon.EquipSprite.Height);
        }


        public override void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(Sprite, Bounds,null
                           , color, (float)GetRotation(),
                           new Vector2(Sprite.Width / 4, Sprite.Height / 4), SpriteEffects.None, priority);
            double temp = (double)Health / (double)maxHealth;
            if (temp >= 0.5)
            {
                spriteBatch.DrawString(world.GetEngine().arialBold, Health.ToString(), new Vector2(Location.X, Location.Y - 30), Color.Green);
            }
            else if (temp < 0.5 && temp >= 0.2)
            {
                spriteBatch.DrawString(world.GetEngine().arialBold, Health.ToString(), new Vector2(Location.X, Location.Y - 30), Color.Goldenrod);
            }
            else spriteBatch.DrawString(world.GetEngine().arialBold, Health.ToString(), new Vector2(Location.X, Location.Y - 30), Color.Red);
        }

        //TEMP METHOD FOR TESTING INVENTORY AND WEAPON GEN
        public void loadRandomInventory()
        {
            Random r = new Random();
            for (int n = 0; n < Inventory.Length; n++)
            {
                Inventory[n] = Weapon.GenRandomWeapon(this.World.Floor, r, this.World);
            }
        }

        public Vector2 getSlope()
        {
            return weaponSlope;
        }

        public void swapWeapon(Weapon w)
        {
            equippedWeapon.InventoryColor = Color.White;
            w.InventoryColor = Color.Yellow;
            equippedWeapon = w;
            weaponSprite = w.EquipSprite;
        }

        public int Jump()
        {
            //jumping = true;
            Line l = (Line)World.objects["jumpLine"];
            double speed = 15;
            double angle = l.rotation;
            Debugger.writeLine((angle * 2 *Math.PI)+" is what direction i'm jumping");
            //Vector2 mousePoint = new Vector2((Mouse.GetState().X + -World.GetEngine().GetCameraPos().X) - Location.X, (Mouse.GetState().Y + -World.GetEngine().GetCameraPos().Y) - Location.Y);
            VX = (float)(speed * Math.Cos(angle));
            VY = (float)(speed * Math.Sin(angle));
            Debugger.writeLine(VX + ", " + VY);
            checkForCollision = false;
            World.GetEngine().prevLength = 5000;
            return (int)Math.Ceiling((decimal)l.line.Width/(decimal)speed);
            //return mousePoint;
            //jumping = false;
            
        }
    }
}
